<!--
 * @description  : 市管-库存产品
 * @descriptionDetail: 市管-库存产品
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2023-06-29 15:43:02
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif --> 
    <view class="content">
      <u-form :model="dataForm" label-width="auto" label-position="left" ref="uForm">
        <view style="width: calc(100% + 30px);">
          <form-section title="基础信息"></form-section>
        </view>
        <u-form-item label="登记人">
          <u-input type="text" input-align="right" :disabled="true" placeholder="请输入" v-model="dataForm.registrantName" />
        </u-form-item>
        <u-form-item label="登记时间">
          <u-input type="text" input-align="right" :disabled="true" placeholder="请输入" v-model="dataForm.createTime" />
        </u-form-item>
        <view style="margin: 0px -18px;">
          <u-gap :height="$theme.cm_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
        </view>
        <view class="m-flex-row">
          <view style="width: calc(100% + 30px);">
            <form-section title="产品信息"></form-section>             
          </view>
          <view style="display: flex;flex-direction: row;align-items: center;">
            <u-icon name="plus-circle" color="primary" size="40" v-if="!isDisabled" @click="addGoods()">
            </u-icon>
          </view>
        </view>
        <u-form-item label-position="top">
          <view class="u-p-b-20" style="width: 100%;" v-show="goodsListShow">
            <u-table font-size="26">
              <u-tr class="u-tr">
                <u-th class="u-th" width="10%">
                  序号
                </u-th>
                <u-th class="u-th">
                  物料类型
                </u-th>
                <u-th class="u-th">
                  物料数量
                </u-th>
                <u-th class="u-th" v-if="!isDisabled" width="15%">
                  操作
                </u-th>
              </u-tr>
              <u-tr class="u-tr" v-for="(item,index) in dataForm.productRecordList" :key="item.recordUuid">
                <u-td class="u-td" width="10%">
                  {{ index+1 }}
                </u-td>
                <u-td class="u-td">
                  <view>
                    <dic-input
                      :keyWord="$u.dic.DIC_KEYS.L_UNLIC_ECIG_ENT_PRODUCT_TYPE"
                      :dicCode="item.materialType"
                      :border="true"
                      type="select"
                      :select-open="dicSelectOpen($u.dic.DIC_KEYS.L_UNLIC_ECIG_ENT_PRODUCT_TYPE)"
                      placeholder="请选择"
                      :disabled="isDisabled"
                      v-model="item.materialTypeName"
                      @changeInputModelData="(name)=>{$set(item, 'materialTypeName', name)}"
                      @click="showDic($u.dic.DIC_KEYS.L_UNLIC_ECIG_ENT_PRODUCT_TYPE,item.materialType,item)"
                    />
                  </view>				  
                </u-td>
                <u-td class="u-td">
                  <view>
                    <u-input type="text" :disabled="isDisabled" :border="true" placeholder="" v-model="item.materialQty" @blur="onModifyMaterialQty(item)" />
                  </view>
                </u-td>
                <u-td class="u-td" style="color: #fa3534;" v-if="!isDisabled" width="15%">
                  <text @click="delTmp(item,index)">
                    删除
                  </text>
                </u-td>
              </u-tr>
            </u-table>
          </view>
        </u-form-item>
      </u-form>
    </view>
    <dic-choice
      :keyWord="dicKeyWords"
      :currentSelect="dicCurrentSelect"
      v-model="dicShow"
      @confirm="selectDicConfirm"
    ></dic-choice>
    <view class="submit-btns btn-other-cls" v-if="!isDisabled">
      <u-button class="u-btn" type="primary" @click="submit">
        保存
      </u-button>
    </view>
  </view>
</template>

<script>
import FormSection from "@/components/form-section.vue";
import DicChoice from "@/components/dic_choice.vue";
import DicInput from "@/components/dic_input.vue";
import checkService from "@/service/check/check.service";
import {
  v4 as uuidv4,
} from 'uuid';
export default {
  components:{
    FormSection,
    "dic-choice": DicChoice,
    "dic-input": DicInput,
  },
  data() {
    return {
      user: this.$store.state.userInfo,
      prevParam:{},
      isDisabled:false,
      dataForm:{
        registrantName: '',
        createTime: this.$u.date(Date.now(), 'yyyy-mm-dd hh:MM:ss'),
        productRecordList:[],
        registerType:'01', // 库存产品
      },
      goodsListShow: true,
      dicShow: false, // dic_select 是否显示
      dicKeyWords: "", // dic 关键字
      dicCurrentSelect: "", // dic 当前选择项
      currGoods:{},
    }
  },
  // 加载初始周期方法
  onLoad() {
    this.prevParam = this.getRouteParam();
    console.log(this.prevParam);
    this.isDisabled = this.prevParam.type == 'view' ? true : false;
    this.dataForm.entUuid = this.prevParam.entUuid;
    // 表单处理
    if (this.prevParam.param) {
      this.getRegisterList();
    }else{
      this.dataForm.registrantName = this.user.user.personName;	
      if(this.prevParam.type == 'add'){
        this.addGoods();  
      }
    }   
  },
  methods: {
    // 信息采集-获取库存产品/库存生产物料数据
    getRegisterList(){
      this.$modal.showLoading('加载中');
      checkService.findStockRegisterById({id: this.prevParam.param.registerUuid,}).then((rs) => {
        this.$modal.hideLoading();
        if(rs.success){
          this.refreshData(rs.bean);
        }else{
          this.$u.toast(rs.msg);
        } 
      }).catch((err) => {
        this.$modal.hideLoading();
        this.$u.toast(err); 
      });
    },  
    refreshData(data){
      this.objMerge(this.dataForm, data);
    },	
    // 字典单选
    dicSelectOpen: function(keyWords) {
      if (this.dicKeyWords == keyWords && this.dicShow) {
        return true;
      }
      return false;
    },
    showDic: function(key, cur,item) {
      if (this.isDisabled) return;
      if (this.dicShow) return;
      this.dicKeyWords = key;
      this.dicCurrentSelect = cur;
      this.dicShow = true;
      this.currGoods = item;
    },
    selectDicConfirm: function(e) {
      // 样品状态
      if (this.dicKeyWords == this.$u.dic.DIC_KEYS.L_UNLIC_ECIG_ENT_PRODUCT_TYPE) {
        this.dataForm.productRecordList.forEach((item,index) => {
          if(item.recordUuid == this.currGoods.recordUuid){
            item.materialTypeName = e[0].label;
            item.materialType = e[0].extra.bcbCode;
          }
        });
      }
    },
    /**
	 * 物料数量 输入数量处理
	 * @param goods
	 */
    onModifyMaterialQty(goods){
      let reg = new RegExp('^\\d+(\\.\\d+)?$');
      if(goods.materialQty != ''){
        // 验证输入的物料数量是否正确
        if(!reg.test(String(goods.materialQty))){
          this.$u.toast("请输入数字！");
          goods.materialQty = '0';
        } else {
          if(Number(goods.materialQty) > 999999){
            this.$u.toast("超出最大样品数量!");
            goods.materialQty = '0';
            return;
          }
          let jy = new Intl.NumberFormat(["cn-ZH",],{
            style:"decimal",
            maximumFractionDigits:2,
          });
          let temp = jy.format(Number(goods.materialQty));
          temp = temp.replace(/,/g,"");
          goods.materialQty = temp;
        }
      }else{
        goods.materialQty = '0';
      }
    },
    addGoods(){
      let nullGoods = {
        recordUuid:uuidv4(),
        materialType:'',
        smaterialTypeName:'',
        materialQty:0,
      }
      this.dataForm.productRecordList.push(nullGoods);
    },
    delTmp(item,index){
      this.dataForm.productRecordList.splice(index,1);
    },
    // 验证库存产品
    validGoods(){
      let flag = true;
      this.dataForm.productRecordList.forEach((item) => {
        if (!item.materialType) {
          this.$u.toast("请选择物料类型！");
          flag = false;
          return false;
        }
        if (!item.materialQty || item.materialQty == '0' || item.materialQty == 0) {
          this.$u.toast("请输入物料数量！");
          flag = false;
          return false;
        }
      });
      return flag;
    },
    submit(){
      if(this.dataForm.productRecordList.length > 0){
        let goodFlag = this.validGoods();
        if(goodFlag){
          this.$modal.showLoading('保存中');
          checkService.saveOrUpdateStockRegister(this.dataForm).then((rs) => {
            this.$modal.hideLoading();  
            if(rs.success){
              this.backPrev({
                paramKey: "formItem",
                param :{
                  type:'products',
                  param: this.dataForm,
                },
              });
            }else{
              this.$u.toast(rs.msg);
            }
          }).catch((err) => {
            this.$modal.hideLoading();
            this.$u.toast(err); 
          });          
        }
      }else{
        this.$u.toast("请添加产品信息!");
        return;
      }

    },
  },
}
</script>

<style scoped lang="scss">
.page{
  padding: 0;
}
.m-flex-row {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.content{
  padding: 0px 10px 65px;;
}
</style>

